package com.fansl.allround.common.log.aspect;

import com.fansl.allround.common.core.util.SpringContextHolder;
import com.fansl.allround.common.log.annotation.SysLog;
import com.fansl.allround.common.log.event.SysLogEvent;
import com.fansl.allround.common.log.util.SysLogUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

/**
 * @author fansl
 * @Description: 操作日志使用spring event异步入库
 * @date 2019/7/11 17:14
 */
@Aspect
@Slf4j
public class SysLogAspect {

    @Around("@annotation(sysLog)")
    @SneakyThrows
    public Object around(ProceedingJoinPoint point,SysLog sysLog){
        String strClassName = point.getTarget().getClass().getName();
        String strMethodName = point.getSignature().getName();
        log.debug("[类名]:{},[方法]:{}",strClassName,strMethodName);

        com.fansl.allround.upms.api.entity.SysLog logVo = SysLogUtils.getSysLog();
        logVo.setTitle(sysLog.value());
        //发送异步日志时间
        Long startTime = System.currentTimeMillis();
        Object obj = point.proceed();
        Long endTime = System.currentTimeMillis();
        logVo.setTime(startTime-endTime);
        SpringContextHolder.publishEvent(new SysLogEvent(logVo));
        return obj;
    }
}
